Exactly What is Hard Coding and Why is it Bad?

Definition:  to fix (data or parameters) in a program in such a way that they cannot be altered without modifying the program code. 

 

Data that pertain to the purpose of a program and has a high likelihood of changing should be stored in variables, not in coding statements.

 

For example:

 

n=100

count=0

for i in range(1,n):

    count = count + 1

 

print(count)

 

In the program above the variable n is hard coded to be 100.

 

A more useful program would be written very similarly but would not contain hard coding:

 

n=int(input('Enter the number to end the count: '))

count=0

for i in range(1,n):

    count = count + 1

 

print(count)

 

Notice the first line of code in this program allowed the variable n to be entered by the user.

 

Hard coding is bad because it forces the programmer to edit the programming code to run the program for a different value of n. Notice that the line count = 0 is not considered hard coding because count (in this program) always needs to begin at 0.